/*
 * nipcManager.c
 *
 *  Created on: 24/09/2011
 *      Author: utn_so
 */

#include "nipcManager.h"
#include <stdlib.h>

char *serializarNipc(int32_t type, char* message,int32_t payloadLength)
{
	PAQUETE_NIPC_CABECERA newMessage;
	newMessage.type = type;
	newMessage.payloadlength = payloadLength;


	char *buffer =malloc(sizeof(PAQUETE_NIPC_CABECERA) + payloadLength);
	memcpy(buffer, &newMessage,sizeof(PAQUETE_NIPC_CABECERA));
/*
	char* payload=malloc(payloadLength);
	memcpy(payload,message,payloadLength);
*/

	memcpy(buffer+sizeof(PAQUETE_NIPC_CABECERA), message, payloadLength);

	return buffer;

}

char *serializarNipcSinPayload(int32_t type,int32_t payloadLength)
{
	PAQUETE_NIPC_CABECERA newMessage;
	newMessage.type = type;
	newMessage.payloadlength = payloadLength;


	char *buffer =malloc(sizeof(PAQUETE_NIPC_CABECERA));
	memcpy(buffer, &newMessage,sizeof(PAQUETE_NIPC_CABECERA));


	return buffer;

}

stPedido *recibirDeSocket(int32_t socket,PAQUETE_NIPC_CABECERA *cabecera,int32_t *total)
{
	stPedido *pedido;
	pedido = (stPedido *)malloc(sizeof(stPedido));
	int32_t nbytes = 0;
	nbytes = recv(socket, pedido, cabecera->payloadlength, MSG_WAITALL);
	*total=nbytes;

	return pedido;
}

